Skip to content

Fix #3912: Account for units and blocks in inline and macros #4012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 21, 2018

Conversation

biboudis
Copy link
Contributor

No description provided.

tree match {
case expansion: Select if expansion.symbol.isSplice =>
Some(expansion)
case Block(List(stat), Literal(Constant(()))) => unapply(stat)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give an example of when this happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is related to the implicit conversion related to value discarding (simply put when an e returns a value and it's called from a function of expected type Unit and it is converted into { e; () }). This manifested in quoted code as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example inline foo: Unit = 4 will become inline foo: Unit = { 4; () } and then be inlined as { 4; () }. In our case we have a ~xyz instead of 4.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! I would add a comment explaining why this is needed (i.e. the compiler can insert ()).

@nicolasstucki nicolasstucki changed the title Fix #3912: account for units and blocks in inline and macros Fix #3912: Account for units and blocks in inline and macros Feb 19, 2018
/** InlineSplice is used to detect cases where the expansion
* consists of a (possibly multiple & nested) block or a sole expression.
*/
object InlineSplice {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how performance sensitive this code is but you could return a Tree instead of Option[Select] to avoid boxing (and EmptyTree instead of None)

Copy link
Contributor Author

@biboudis biboudis Feb 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need that .get method on unapply's return type so isn't Option unavoidable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe Tree qualifies for both product pattern and name based pattern. And scalac supports name based pattern

@odersky
Copy link
Contributor

odersky commented Feb 21, 2018

What's the relationship with #4019?

@nicolasstucki
Copy link
Contributor

No relationship.

@nicolasstucki
Copy link
Contributor

Needs rebase

// To maintain phase consistency, we move the binding of the this parameter into the spliced code
val (splicedBindings, stagedBindings) = bindings.partition {
case vdef: ValDef => vdef.symbol.is(Synthetic) // Assume that only _this bindings are tagged with Synthetic
case _ => false
}

val tree1 =
if (level == 0) cpy.Inlined(tree)(call, stagedBindings, Splicer.splice(seq(splicedBindings, body)))
else seq(stagedBindings, cpy.Select(expansion)(cpy.Inlined(tree)(call, splicedBindings, body), name))
transform(tree1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be atree2

val tree2 = transform(tree1)

val tree1 =
if (level == 0) cpy.Inlined(tree)(call, stagedBindings, Splicer.splice(seq(splicedBindings, body)))
else seq(stagedBindings, cpy.Select(expansion)(cpy.Inlined(tree)(call, splicedBindings, body), name))
transform(tree1)

// due to value-discarding which converts an { e } into { e; () })
if (tree.tpe =:= defn.UnitType) Block(tree1 :: Nil, Literal(Constant(())))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And use tree2 here and in next line

@biboudis
Copy link
Contributor Author

Approve @nicolasstucki? ⭐️

@nicolasstucki nicolasstucki merged commit 2a85106 into scala:master Feb 21, 2018
@Blaisorblade Blaisorblade deleted the fix-#3912 branch February 21, 2018 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants